package com.ztgf.sms.scheduled;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xuanwu.mos.common.entity.GsmsResponse;
import com.ztgf.sms.common.GetXWPostMsg;
import com.ztgf.sms.models.dto.sms.SmsInfoDTO;
import com.ztgf.sms.models.service.IEmailInfoService;
import com.ztgf.sms.models.service.ISmsInfoService;
import com.ztgf.sys.models.dto.SysEmailManageDTO;
import com.ztgf.sys.models.service.ISysEmailManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * @Description ：短信发布定时任务
 * @Author ： liyang
 * @Date ： 2021-05-14
 */
@Component
@Slf4j
public class ScheduledTimes {


    @DubboReference
    private ISmsInfoService iSmsInfoService;

    @DubboReference
    private ISysEmailManageService iSysEmailManageService;

    @DubboReference
    private IEmailInfoService iEmailInfoService;

    @Autowired
    private GetXWPostMsg getXWPostMsg;

    /**
     * 每1分钟检测短信批量发送，
     */
    @Scheduled(cron="0 0/1 * * * ?")
    public void executeSMSUpdate()  {
        Wrapper<SmsInfoDTO> wrapper = new EntityWrapper<> ();
        wrapper.eq("status", 1)
                 .eq ("type",2);//判断后台推送的和发送失败的

        List<SmsInfoDTO> dtos = iSmsInfoService.selectList (wrapper);
        if (!CollectionUtils.isEmpty(dtos)){
            for (SmsInfoDTO smsInfoDTO: dtos) {
                HashMap<String, String> batchMap = new HashMap<>();
                batchMap.put ("mobile" ,smsInfoDTO.getMobile ());
                batchMap.put ("content" ,smsInfoDTO.getContent ());
                GsmsResponse resp = null; //发送短信
                log.info ("测试每分钟检测短信发送状态定时任务------------------------->" + JSON.toJSONString (smsInfoDTO));
                try {
                    resp = getXWPostMsg.sendBatchSMS(batchMap);
                    if (resp.getResult() == 0) {
                        smsInfoDTO.setStatus(0);//发送状态置为成功
                    } else {
                        smsInfoDTO.setStatus(1);//发送状态置为失败
                    }
                } catch (Exception e) {
                    log.info ("短信发送一次++++++++++" + smsInfoDTO.getName ());
                    e.printStackTrace ();
                }
                iSmsInfoService.updateById(smsInfoDTO);
            }
        }

    }

    /**
     * 每1分钟检测邮件批量发送，
     */
    @Scheduled(cron="0 0/1 * * * ?")
    public void executeEmailUpdate()  {
        Wrapper<SysEmailManageDTO> wrapper = new EntityWrapper<> ();
        wrapper.eq("status", 1);//搜索发送失败的
        List<SysEmailManageDTO> dtos = iSysEmailManageService.selectList (wrapper);
        if(!CollectionUtils.isEmpty(dtos)){
            for (SysEmailManageDTO sysEmailManageDTO: dtos) {
                String title = sysEmailManageDTO.getMailTitle ();
                String context = sysEmailManageDTO.getMailContent ();
                String emails = sysEmailManageDTO.getEmail ();
                String[] emailsArr = emails.split (",");
                try {
                    boolean flag = iEmailInfoService.sendMailByScheduled(title,context,emailsArr);
                    if (flag){
                        sysEmailManageDTO.setStatus (0);
                    }
                } catch (Exception e){
                    log.error("邮件批量发送 失败",e);
                }
                iSysEmailManageService.updateById(sysEmailManageDTO);

            }
        }

    }
}


